package com.zzx.schoolfellow.common.api;

import lombok.Data;

@Data
public class CommonResponse<T> {
    private Integer code;

    private String msg;

    private T data;

    public static CommonResponse success() {
        CommonResponse response = new CommonResponse();
        response.setCode(ResponseCodeEnum.SUCCESS.getCode());
        response.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        return response;
    }

    public static <T> CommonResponse<T> success(T data) {
        CommonResponse response = success();
        response.setData(data);
        return response;
    }

    public static CommonResponse fail(ResponseCodeEnum codeEnum) {
        CommonResponse response = new CommonResponse();
        response.setCode(codeEnum.getCode());
        response.setMsg(codeEnum.getMsg());
        return response;
    }

    public static CommonResponse fail(Integer code, String msg) {
        CommonResponse response = new CommonResponse();
        response.setCode(code);
        response.setMsg(msg);
        return response;
    }

    public static CommonResponse result(boolean result) {
        if (result) {
            return success();
        } else {
            return fail(ResponseCodeEnum.FAILURE);
        }
    }

}
